package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupRelationDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupRelationService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 21:46:46<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_RELATION_ACTION_PUSH)
public class GroupRelationAction {

	@Resource
	private GroupRelationService groupRelationService;

	/**
	 * 计数
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage getCount(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("count", groupRelationService.count(R.param("userId", userId))));
	}

	/**
	 * 列表
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage getList(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("list", groupRelationService.getListByUserId(userId)));
	}

	/**
	 * 获取详情
	 */
	@ActionMapping(value = "1.1.0004")
	public ResultMessage getRelation(SocketSession socketSession, @JsonDefine("data") GroupRelationDto query) {
		if (NumUtil.isInValid(query.getGroupId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long ownerUserId = socketSession.getKey();
		return new ResultMessage(R.param("data", groupRelationService.getByGroupId(ownerUserId, query.getGroupId())));
	}

	/**
	 * Date 2019-01-20 17:37:06<br>
	 * Description 修改群备注
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0005")
	public ResultMessage updateRemark(SocketSession socketSession, HeadImpl head, @JsonDefine("data") GroupRelationDto query) {
		if (NumUtil.isInValid(query.getGroupId()) || YitunStringUtil.isEmpty(query.getRemark()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long userId = socketSession.getKey();
		return groupRelationService.updateRemark(head.getKey(),userId, query.getGroupId(), query.getRemark());
	}

	/**
	 * Date 2019-01-20 17:37:33<br>
	 * Description 移动群
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage moveCategory(SocketSession socketSession, HeadImpl head, @JsonDefine("data") GroupRelationDto query) {
		if (NumUtil.isInValid(query.getCategoryId()) || YitunCollectionUtil.isEmpty(query.getGroupIds()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		Long userId = socketSession.getKey();
		return groupRelationService.moveCategory(head.getKey(),userId, query.getGroupIds(), query.getCategoryId());
	}
}
